#define DEBUG
#include <cstdio>
#define MAXN 100000
#define MAXM 60

using namespace std;

class Graph {
public:
  int n, m, ind[MAXN+1], to[2*MAXN-1], val[2*MAXN-1], link[2*MAXN-1], dep[MAXN+1], fa[MAXN+1][2];

  void addSide(int u, int v, int l) {
    m++;
    to[m] = v;
    val[m] = l;
    link[m] = ind[u];
    ind[u] = m;
  }

  void build(int o, int fa) {
    if (fa>=0) {
      dep[o] = dep[fa]+1;
    } else {
      dep[o] = 1;
    }
    for (int i=ind[o]; i; i=link[i]) {
      if (to[i]!=fa) {
	build(to[i], o);
	this->fa[to[i]][0] = o;
	this->fa[to[i]][1] = val[i];
      }
    }
  }

  int lca(int u, int v) {
    int ret;
    if (dep[u]<dep[v]) {
      ret=u, u=v, v=ret;
    }
    ret = 0;
    for (; dep[u]>dep[v]; u=fa[u][0]) {
      ret += fa[u][1];
    }
    for (; u!=v; u=fa[u][0], v=fa[v][0]) {
      ret += fa[u][1]+fa[v][1];
    }
    return ret;
  }
};

int main() {
#ifdef DEBUG
  freopen("0.in", "r", stdin);
  freopen("0.out", "w", stdout);
#endif

  static Graph g[MAXM+1];
  int t;
  scanf("%d", &t);
  g[0].n = 1;
  g[0].build(0, -1);
  for (int ti=1; ti<=t; ti++) {
    int m;
    scanf("%d", &m);
    for (int i=1; i<=m; i++) {
      int a, b, c, d, l;
      scanf("%d %d %d %d %d", &a, &b, &c, &d, &l);
      g[i] = g[a];
      for (int j=0; j<g[b].n; j++) {
	for (int k=g[b].ind[j]; k; k=g[b].link[k]) {
	  g[i].addSide(j+g[a].n, g[b].to[k]+g[a].n, g[b].val[k]);
	}
      }
      g[i].n = g[a].n+g[b].n;
      g[i].addSide(c, g[a].n+d, l);
      g[i].addSide(g[a].n+d, c, l);
      g[i].build(0, -1);
      int ans=0;
      for (int j=0; j<g[i].n; j++) {
	for (int k=j+1; k<g[i].n; k++) {
	  ans += g[i].lca(j, k);
	}
      }
      printf("%d\n", ans);
    }
  }

  fcloseall();
  return 0;
}
